package services;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.SqlSession;

import play.libs.Codec;
import utils.BizConstants;

import com.aton.db.SessionFactory;

import models.DrCase;
import models.DrCaseConfig;
import models.DrCaseContent;
import models.DrCaseContentExample;
import models.DrCaseExample;
import models.DrVCase;
import models.DrVCaseTemp;
import models.DrWords;
import models.SystemLog;
import models.SystemOperator;
import models.mapper.DrCaseContentMapper;
import models.mapper.DrCaseMapper;
import models.mapper.DrWordsMapper;
import models.mapper.SystemLogMapper;

public class DrCaseContentService {
	
	private static final SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd");
	
	public static boolean saveCaseRecordContent(DrCaseContent drCaseContent, SystemOperator loginUser,
			String ip) {
		if(getByCaseid(drCaseContent.getCaseid()) == null){
			addCaseRecordContent(drCaseContent, loginUser, ip);
		}else{
			updateCaseRecordContent(drCaseContent, loginUser, ip);
		}
		return Boolean.TRUE;
	}
	
	public static boolean addCaseRecordContent(DrCaseContent drCaseContent, SystemOperator loginUser,
			String ip){
		SqlSession ss = SessionFactory.INSTANCE.getSqlSessionWithoutAutoCommit();
		try{
			DrCaseContentMapper mapper=ss.getMapper(DrCaseContentMapper.class);
			mapper.insert(drCaseContent);
			//插入日志
			SystemLog log=new SystemLog(Codec.UUID(),loginUser.getOperatorid(),ip,"添加庭审笔录","添加庭审笔录",new Date());
			SystemLogMapper logMapper=ss.getMapper(SystemLogMapper.class);
			logMapper.insert(log);
			ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			ss.rollback();
			return false;
		}finally{
			ss.close();
		}
		return Boolean.TRUE;
	}
	
	public static DrCaseContent getByCaseid(String caseid){
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.INSTANCE.getSqlSession();
		try{
			DrCaseContentMapper caseMapper=ss.getMapper(DrCaseContentMapper.class);
			
			return caseMapper.getByCaseid(caseid);
		}finally{
			ss.close();
		}
	}
	
	public static boolean updateCaseRecordContent(DrCaseContent drCaseContent, SystemOperator loginUser,
			String ip){
		SqlSession ss = SessionFactory.INSTANCE.getSqlSessionWithoutAutoCommit();
		try{
			DrCaseContentMapper caseMapper=ss.getMapper(DrCaseContentMapper.class);
			DrCaseContentExample example=new DrCaseContentExample();
			DrCaseContentExample.Criteria c=example.createCriteria();
			c.andCaseidEqualTo(drCaseContent.getCaseid());
			
			caseMapper.updateByExampleSelective(drCaseContent, example);
			SystemLog log=new SystemLog(Codec.UUID(),loginUser.getOperatorid(),ip,"更新庭审笔录","更新庭审笔录",new Date());
			SystemLogMapper logMapper=ss.getMapper(SystemLogMapper.class);
			logMapper.insert(log);
			ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			ss.rollback();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 根据案件编号获取
	 * @param caseid 案件编号
	 * @return
	 */
	public static DrCaseContent getById(String caseid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.INSTANCE.getSqlSession();
		try{
			DrCaseContentMapper caseMapper=ss.getMapper(DrCaseContentMapper.class);
			return caseMapper.selectByPrimaryKey(caseid);
		}finally{
			ss.close();
		}
	}
	/**
	 * 获取庭审初始化信息
	 */
	public static String combineContent(String caseid) {
		// TODO Auto-generated method stub
		DrVCaseTemp casetemp=CaseService.getTempViewById(caseid);
		DrVCase cases=new DrVCase();
		if(casetemp==null){
			cases=CaseService.getViewById(caseid);
		}
		else{
			cases=new DrVCase(casetemp);
		}
		List<DrCaseConfig> configs=DrCaseConfigService.getByCaseid(caseid);
		String courtName=SystemParamService.getValueByCacheId(BizConstants.PARAM_MAP.get("COURT_NAME"));
		String content="<p style='font-size:22px;text-align:center;font-color:#0b0b0b'>"+courtName+
						"</p>"+
						"<p style='font-size:22px;margin-left:20px;font-color:#0b0b0b'>"+
						"<span>法庭审理笔录</span><span style='margin-left:40px'>(第一次)</span></p>";
						
		if(StringUtils.isNotEmpty(cases.getCasecode())){
			content+="<p style='margin-left:20px'>案件编号："+cases.getCasecode()+"</p>";
		}
		if(StringUtils.isNotEmpty(cases.getCasename())){
			content+="<p style='margin-left:20px'>案件名称："+cases.getCasename()+"</p>";
		}
		if(cases.getOpendate()!=null){
			content+="<p style='margin-left:20px'>开庭时间："+dateFormater.format(cases.getOpendate())+"</p>";
		}
		if(StringUtils.isNotEmpty(cases.getOpencourt())){
			content+="<p style='margin-left:20px'>开庭地点："+cases.getOpencourt()+"</p>";
		}
		if(StringUtils.isNotEmpty(cases.getJudgerchief())){
			content+="<p style='margin-left:20px'>审判长："+cases.getJudgerchief()+"</p>";
		}
		if(StringUtils.isNotEmpty(cases.getJudger())){
			content+="<p style='margin-left:20px'>审判员："+cases.getJudger()+"</p>";
		}
		if(StringUtils.isNotEmpty(cases.getClerker())){
			content+="<p style='margin-left:20px'>书记员："+cases.getClerker()+"</p>";
		}
		if(StringUtils.isNotEmpty(cases.getPublictext())){
			content+="<p style='margin-left:20px'>是否公开："+cases.getPublictext()+"</p>";
		}
		if(StringUtils.isNotEmpty(cases.getSecrettext())){
			content+="<p style='margin-left:20px'>密级："+cases.getSecrettext()+"</p>";
		}
		if(configs!=null&&configs.size()>0){
			for(DrCaseConfig config : configs){
				content+="<p style='margin-left:20px'>"+config.getConfigname()+":"+config.getValue()+"</p>";
			}
		}
		return content;
	}
}
